package com.tuanzi.loan.business.repository.settlementsystem.impl;

import com.tuanzi.loan.business.repository.settlementsystem.custom.SettlementIncomeRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.settlementsystem.SettlementIncomeRequest;
import com.tuanzi.loan.vo.response.settlementsystem.SettlementIncomeResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementIncome.LOAN_SETTLEMENT_INCOME;

/**
 * @author caiwanlin
 * @create 2018-08-16 16:21
 */
public class SettlementIncomeRepositoryImpl extends JOOQBaseRepositoryPlus implements SettlementIncomeRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_INCOME;
    }

    @Override
    public Page<SettlementIncomeResponse> list(SettlementIncomeRequest searchRequest, Pageable pageable) {
        Long productId = searchRequest.getProductId();
        Long companyId = searchRequest.getCompanyId();
        Integer spreadMode = searchRequest.getSpreadMode();
        Integer spreadChannel = searchRequest.getSpreadChannel();
        Integer copMode = searchRequest.getCopMode();
        Integer settlementMode = searchRequest.getSettlementMode();
        Integer billStatus = searchRequest.getBillStatus();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        String creator = searchRequest.getCreator();
        Integer incomeType = searchRequest.getIncomeType();

        fields(LOAN_SETTLEMENT_INCOME.fields());
        eq(LOAN_SETTLEMENT_INCOME.PRODUCT_ID,productId);
        eq(LOAN_SETTLEMENT_INCOME.COMPANY_ID,companyId);
        eq(LOAN_SETTLEMENT_INCOME.SPREAD_MODE,spreadMode);
        eq(LOAN_SETTLEMENT_INCOME.SPREAD_CHANNEL,spreadChannel);
        eq(LOAN_SETTLEMENT_INCOME.COP_MODE,copMode);
        eq(LOAN_SETTLEMENT_INCOME.SETTLEMENT_MODE,settlementMode);
        eq(LOAN_SETTLEMENT_INCOME.BILL_STATUS,billStatus);
        eq(LOAN_SETTLEMENT_INCOME.INCOME_TYPE,incomeType);
        between(LOAN_SETTLEMENT_INCOME.START_TIME,startTime,endTime);
        eq(LOAN_SETTLEMENT_INCOME.CREATOR,creator);
        orderBy(LOAN_SETTLEMENT_INCOME.CREATE_TIME, Sort.Direction.DESC);
        Page<SettlementIncomeResponse> search = search(pageable, record -> {
            SettlementIncomeResponse response = new SettlementIncomeResponse();
            response.setId(record.get(LOAN_SETTLEMENT_INCOME.ID));
            response.setProductId(record.get(LOAN_SETTLEMENT_INCOME.PRODUCT_ID));
            response.setProductName(record.get(LOAN_SETTLEMENT_INCOME.PRODUCT_NAME));
            response.setCompanyId(record.get(LOAN_SETTLEMENT_INCOME.COMPANY_ID));
            response.setCompanyName(record.get(LOAN_SETTLEMENT_INCOME.COMPANY_NAME));
            response.setIncomeType(record.get(LOAN_SETTLEMENT_INCOME.INCOME_TYPE));
            response.setStartTime(record.get(LOAN_SETTLEMENT_INCOME.START_TIME));
            response.setEndTime(record.get(LOAN_SETTLEMENT_INCOME.END_TIME));
            response.setSpreadMode(record.get(LOAN_SETTLEMENT_INCOME.SPREAD_MODE));
            response.setSpreadChannel(record.get(LOAN_SETTLEMENT_INCOME.SPREAD_CHANNEL));
            response.setCopMode(record.get(LOAN_SETTLEMENT_INCOME.COP_MODE));
            response.setPriceCpa(record.get(LOAN_SETTLEMENT_INCOME.PRICE_CPA));
            response.setPriceCps(record.get(LOAN_SETTLEMENT_INCOME.PRICE_CPS));
            response.setRegisterCount(record.get(LOAN_SETTLEMENT_INCOME.REGISTER_COUNT));
            response.setClickCount(record.get(LOAN_SETTLEMENT_INCOME.CLICK_COUNT));
            response.setLendAmount(record.get(LOAN_SETTLEMENT_INCOME.LEND_AMOUNT));
            response.setIncomeAmount(record.get(LOAN_SETTLEMENT_INCOME.INCOME_AMOUNT));
            response.setParticipateUv(record.get(LOAN_SETTLEMENT_INCOME.PARTICIPATE_UV));
            response.setUvValue(record.get(LOAN_SETTLEMENT_INCOME.UV_VALUE));
            response.setSettlementMode(record.get(LOAN_SETTLEMENT_INCOME.SETTLEMENT_MODE));
            response.setPayMode(record.get(LOAN_SETTLEMENT_INCOME.PAY_MODE));
            response.setBillId(record.get(LOAN_SETTLEMENT_INCOME.BILL_ID));
            response.setBillStatus(record.get(LOAN_SETTLEMENT_INCOME.BILL_STATUS));
            response.setRemark(record.get(LOAN_SETTLEMENT_INCOME.REMARK));
            response.setCreator(record.get(LOAN_SETTLEMENT_INCOME.CREATOR));
            response.setCreateTime(record.get(LOAN_SETTLEMENT_INCOME.CREATE_TIME));
            response.setUpdator(record.get(LOAN_SETTLEMENT_INCOME.UPDATOR));
            response.setUpdateTime(record.get(LOAN_SETTLEMENT_INCOME.UPDATE_TIME));
            response.setVersion(record.get(LOAN_SETTLEMENT_INCOME.VERSION));
            return response;
        });

        return search;
    }
}
